package com.andy.blog.admin.service.impl;

import com.andy.blog.admin.dto.MessageDTO;
import com.andy.blog.admin.service.MessageBusService;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.common.utils.ip.AddressUtils;
import com.andy.blog.common.utils.ip.IpUtils;
import com.andy.blog.dal.domain.MessageDO;
import com.andy.blog.dal.service.MessageDalService;
import com.andy.blog.dal.vo.MessageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023/8/5 07:49
 */
@Service
public class MessageBusServiceImpl implements MessageBusService {


    @Resource
    private MessageDalService messageDalService;


    @Override
    public void addMessage(MessageVO messageVO) {
        messageVO.setIpLocation(AddressUtils.getProvinceByIp(IpUtils.getIpAddress()));
        messageDalService.insertMessage(messageVO);
    }

    @Override
    public List<MessageDTO> listMessage(MessageVO messageVO) {
        List<MessageDO> messageDOS = messageDalService.selectList(messageVO);
        return TransformUtils.transformList(messageDOS, MessageDTO.class);
    }

    @Override
    public PageUtilsDTO<MessageDTO> selectPage(MessageVO messageVO) {
        IPage<MessageDO> messageDOIPage = messageDalService.selectPage(messageVO);
        return PageUtilsDTO.build(messageDOIPage.getTotal(), TransformUtils.transformList(messageDOIPage.getRecords(), MessageDTO.class));
    }

    @Override
    public void deleteById(Long id) {
        messageDalService.deleteById(id);
    }
}
